package com.runshu.webserver.impl;

import com.runshu.rest.RestClient;
import com.runshu.utils.Constant;
import com.runshu.webserver.IWebServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List;

/**
 * 氚云webservice实现
 *
 * @author don-m
 * @date 2020-05-03
 */
@WebService(serviceName = "IWebServer"//服务名
        ,targetNamespace = "http://service.cxf.runshu.com/" //报名倒叙，并且和接口定义保持一致
        ,endpointInterface = "com.runshu.webserver.IWebServer")//包名
@Component
@Slf4j
public class WebServerImpl implements IWebServer {

    @Autowired
    private RestClient restClient;

    @Override
    public String GetSchema(String schemaCode) {
        String result = " {\"Code\":\"D001062ceshi2\",\"Items\":[{\"Name\":\"F0000001\",\"DisplayName\":\"单行文本\",\"DataType\":\"String\"},]}";
        log.info(result);
        return result;
    }

    @Override
    public String GetSchemaList() {
        String result = "{\"customer\": \"测试\",\"D001062ceshi2\": \"测试\"}";
        log.info(result);
        return result;
    }

    @Override
    public String GetList(String userCode, String schemaCode, String filter) {
        String result = "{\"Code\":\"0\",\"Message\":\"获取数据成功\",\"Schema\":{\"Code\":\"D001062ceshi2\",\"Items\":[{\"name\":\"F0000001\",\"DisplayName\":\"单行文本\",\"DataType\":\"String\"}],},\"Data\":[\"F0000001\":\"c而是\"]}";
        log.info(result);
        return result;
    }

    @Override
    public String Invoke(String userCode, String schemaCode, String methodName, String param) {

        log.info("接收到的参数: " + param);

        String apiPath = Constant.API_HOST + methodName;

        log.info("请求API的接口地址为：" + apiPath);
        log.info("开始请求API接口..........");

        try {
            // 调用http接口
            String res = restClient.post(
                    apiPath, // 接口地址
                    String.class, //返回值类型
                    param
            );
            log.info("请求API成功，API接口返回数据为：" + res);
            return res;
        }catch(Exception e) {
            log.info("远程调用出现异常，请检查远程调用工具");
            e.printStackTrace();
            return null;
        }

    }


    @Override
    public List<String> getStudentInfo() {
        List<String> list = new ArrayList<>();
        list.add("213213");
        list.add("dlfjsadkf");
        list.add("dhs");
        return list;
    }
}
